
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int a,b,d,s,i = 0,max = -1,w,v,e,a1,a2,a3,a4,a123,a234,a12,a23,a34,p,m,n,c,g,f,a1234,t;
    cin >> a;
    d = a;
    while (a != 0){
        b = a % 10;
        if (a <= 9)
        a1 = b;
        if (a > 9)
        a2 = b;
        if (a > 99)
        a3 = b;
        if (a > 999)
        a4 = b;
        a = a / 10;
        s++;
    }
    if (s == 4 && d > 0){
            a123 = (a1 * 100) + (a2 * 10) + a3;
            a1234 = (a1 * 1000) + (a2 * 100) + (a3 * 10) + a4;
    a234 = (a2 * 100) + (a3 * 10) + a4;
    a12 = (a1 * 10) + a2;
    a23 = (a2 * 10) + a3;
    a34 = (a3 * 10) + a4;
    for (i = 2; i <= sqrt(a1234);i++){
        if (a1234 % i == 0)
        t++;
    }
    for (i = 2; i <= sqrt(a1);i++){
        if (a1 % i == 0)
        p++;
    }
    for (i = 2; i <= sqrt(a2);i++){
        if (a2 % i == 0)
        w++;
    }
    for (i = 2; i <= sqrt(a3);i++){
        if (a3 % i == 0)
        e++;
    }
    for (i = 2; i <= sqrt(a4);i++){
        if (a4 % i == 0)
        v++;
    }
    for (i = 2; i <= sqrt(a123);i++){
        if (a123 % i == 0)
        m++;
    }
    for (i = 2; i <= sqrt(a234);i++){
        if (a234 % i == 0)
        n++;
    }
    for (i = 2; i <= sqrt(a12);i++){
        if (a12 % i == 0)
        c++;
    }
    for (i = 2; i <= sqrt(a23);i++){
        if (a23 % i == 0)
        f++;
    }
    for (i = 2; i <= sqrt(a34);i++){
        if (a34 % i == 0)
        g++;
    }
    if (p == 0 && a1 > max)
    max = a1;
    if (t == 0 && a1234 > max)
    max = a1234;
    if (w == 0 && a2 > max)
    max = a2;
    if (e == 0 && a3 > max)
    max = a3;
    if (v == 0 && a4 > max)
    max = a4;
    if (m == 0 && a123 > max)
    max = a123;
    if (n == 0 && a234 > max)
    max = a234;
    if (c == 0 && a12 > max)
    max = a12;
    if (f == 0 && a23 > max)
    max = a23;
    if (g == 0 && a34 > max)
    max = a34;
    if (max == -1)
    cout << "0";
    else
    cout << max;
    }
    else
    cout << "0";
}